package com.xique.park.service.biz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.domain.ParkLane;
import com.xique.park.bean.request.web.ipc.ParkIpcListRequest;
import com.xique.park.bean.transform.web.ipc.ParkIpcTransform;
import com.xique.park.bean.vo.web.ipc.ParkIpcVo;
import com.xique.park.constant.message.LaneDtConstants;
import com.xique.park.service.biz.service.IParkIpcBizService;
import com.xique.park.service.service.IParkDeviceNewService;
import com.xique.park.service.service.IParkLaneService;
import com.xique.park.utils.RedisHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 相机bizService
 *
 * @author xique
 */
@Slf4j
@Service
public class ParkIpcBizServiceImpl implements IParkIpcBizService {

    @Autowired
    private IParkDeviceNewService parkDeviceNewService;

    @Autowired
    private IParkLaneService parkLaneService;

    @Autowired
    private RedisHandler redisHandler;

    @Override
    public AjaxResult getParkIpcList(ParkIpcListRequest request) {
        request.setParkId(SecurityUtils.getParkId());
        IPage<ParkDeviceNew> page = parkDeviceNewService.selectParkIpcPage(request);
        List<ParkDeviceNew> parkDeviceNewList = page.getRecords();
        List<ParkIpcVo> parkIpcVoList = parkDeviceNewList.stream().map(parkDeviceNew -> {
            ParkIpcVo parkIpcVo = ParkIpcTransform.transformParkIpcVo(parkDeviceNew);
            JSONObject laneSenseJsonObj = redisHandler.getLaneSense(parkDeviceNew.getLaneId().intValue());
            // 获取获取通道的最新地感状态和道闸状态 BY WLD 2022/4/15
            log.info("[获取通道的最新地感状态和道闸状态]通道ID：{}，查询结果：{}", parkDeviceNew.getLaneId().intValue(), laneSenseJsonObj);
            if (null != laneSenseJsonObj) {
                // 通道地感状态 1：地感检测当前有车；0：地感检测当前无车
                Integer isSense = laneSenseJsonObj.getInteger(LaneDtConstants.IS_SENSE);
                isSense = null == isSense ? -1 : isSense;
                parkIpcVo.setIsSense(isSense);
                // 通道道闸状态 1：道闸关；0：道闸开
                Integer isGateState = laneSenseJsonObj.getInteger(LaneDtConstants.IS_GATE_STATE);
                isGateState = null == isGateState ? -1 : isGateState;
                parkIpcVo.setIsGateState(isGateState);
            } else {
                parkIpcVo.setIsSense(-1);
                parkIpcVo.setIsGateState(-1);
            }
            return parkIpcVo;
        }).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>(2);
        map.put("list", parkIpcVoList);
        map.put("total", page.getTotal());
        return AjaxResult.success(map);
    }

    @Override
    public ParkLane getIpcLane(Long parkId, String sn) {
        ParkDeviceNew parkDeviceNew = new ParkDeviceNew();
        parkDeviceNew.setParkId(parkId);
        parkDeviceNew.setCode(sn);
        List<ParkDeviceNew> parkDeviceNewList = parkDeviceNewService.selectParkDeviceNewList(parkDeviceNew);
        if (CollectionUtils.isNotEmpty(parkDeviceNewList)) {
            ParkDeviceNew parkDeviceNew1 = parkDeviceNewList.get(0);
            return parkLaneService.selectParkLaneById(parkDeviceNew1.getLaneId());
        } else {
            return null;
        }
    }

}
